const { Model, DataTypes, Sequelize } = require("sequelize");
const User = require("./User");
const sequelize = require('../db/index.js');
class Order extends Model {};

Order.init({
    orderId: {
        type: DataTypes.BIGINT,
        defaultValue: () => {
            return Date.now();
        },
        primaryKey: true
    },
    total: {
        type: DataTypes.DECIMAL(10, 2),
        allowNull: false,
    },
    paymentStatus: {
        type: DataTypes.ENUM('pending_payment', 'paid', 'cancelled', 'payment_failed', 'refounded', 'exchanged'), // 待支付、已支付、已取消、支付失败、已退款、积分兑换
        defaultValue: 'pending_payment',
        validate: {
            isIn: [['pending_payment', 'paid', 'cancelled', 'payment_failed', 'refounded', 'exchanged']]
        }
    },
    paymentMethod: {
        type: DataTypes.ENUM('weixin', 'alipay'),
        defaultValue: 'alipay',
        validate: {
            isIn: [['weixin', 'alipay']]
        }
    }
}, {
    sequelize,
    timestamps: true,
    createdAt: 'created_at',
    updatedAt: 'updated_at',
    deletedAt: 'deleted_at',
    paranoid: true,
    defaultScope: {
        order: [['created_at', 'DESC']], // 设置默认排序
    },
});

Order.belongsTo(User, { foreignKey: 'userId' })

module.exports = Order;